<?php

class ShoppingCartController extends Zend_Controller_Action
{
    /**
     * @return void
     */
    public function init()
    {
        /** @var Zend_Controller_Action_Helper_AjaxContext $ajaxContext */
        $ajaxContext = $this->_helper->getHelper('AjaxContext');
        $ajaxContext->addActionContext('edit-name', 'json')
            ->addActionContext('edit-product-amount', 'json')
            ->initContext();
    }

    /**
     * @return void
     */
    public function indexAction()
    {
        /** @var Zend_Controller_Action_Helper_FlashMessenger $flashMessenger */
        $flashMessenger = $this->_helper->getHelper('FlashMessenger');
        $messages = $flashMessenger->getMessages();
        $this->view->messages = $messages;

        $shoppingCart = JFactory::getShoppingCart();
        if ($shoppingCart->IsEmpty) {
            $flashMessenger->addMessage('Your shopping cart have no product');
            $this->redirect('/index/index');
        }

        $name = $shoppingCart->Name;
        $this->view->name = $name;

        $productItems = $shoppingCart->ProductItems;
        $this->view->productItems = $productItems;

        $totalCost = $shoppingCart->TotalCost;
        $this->view->totalCost = $totalCost;
    }

    /**
     * @return void
     */
    public function addProductAction()
    {
        $productID = $this->_request->getParam('productID');
        $shoppingCart = JFactory::getShoppingCart();
        $shoppingCart->addProductItem($productID);
        $this->redirect('/shopping-cart/index');
    }

    /**
     * @return void
     */
    public function deleteProductAction()
    {
        $productItemID = $this->_request->getParam('productItemID');
        $shoppingCart = JFactory::getShoppingCart();
        $shoppingCart->deleteProductItem($productItemID);
        $this->redirect('/shopping-cart/index');
    }

    /**
     * @return void
     */
    public function editNameAction()
    {
        $shoppingCartName = $this->_request->getParam('shoppingCartName');
        if (isset($shoppingCartName)) {
            $shoppingCart = JFactory::getShoppingCart();
            $shoppingCart->Name = $shoppingCartName;
        }
    }

    /**
     * @return void
     */
    public function editProductAmountAction()
    {
        $productItemID = $this->_request->getParam('productItemID');
        $productItemAmount = $this->_request->getParam('productItemAmount');
        if (isset($productItemID) && isset($productItemAmount)) {
            $shoppingCart = JFactory::getShoppingCart();

            $shoppingCart->editAmountProductItem($productItemID, $productItemAmount);
            $productItem = $shoppingCart->getProductItem($productItemID);

            $this->view->productItemTotalCost = $productItem->TotalCost;
            $this->view->shoppingCartTotalCost = $shoppingCart->TotalCost;
        }
    }

    /**
     * @return void
     */
    public function orderAction()
    {
        /** @var Zend_Controller_Action_Helper_FlashMessenger $flashMessenger */
        $flashMessenger = $this->_helper->getHelper('FlashMessenger');
        $result = OrderService::order();
        switch ($result) {
            case OrderResult::FAILURE_USER_NOT_LOGIN:
                $flashMessenger->addMessage('You Must Login!!!');
                $this->redirect('/shopping-cart/index');
                break;
            case OrderResult::FAILURE_USER_UNCONFIRM:
                $flashMessenger->addMessage('Account Must Be Confirmed!');
                $this->redirect('/shopping-cart/index');
                break;
            case OrderResult::FAILURE_CART_EMPTY:
                $flashMessenger->addMessage('The Shopping Cart Must Not Empty!');
                $this->redirect('/shopping-cart/index');
                break;
            case OrderResult::SUCCESS:
                $flashMessenger->addMessage('Order Successfully!');
                $this->redirect('/index/index');
                break;
        }
    }
}





